<template>
	<common-page>
		<div style="padding-top: 3%;">

			<h2 v-if="stepActive==0" class="common-title" style="text-align: center;">密码修改</h2>

			<el-alert v-if="stepActive==0" title="密码已过期，你需要修改密码后才能继续" type="warning" center :closable="false" style="line-height: 30px;" />

			<el-form v-if="stepActive==0" ref="form" :model="form" :rules="rules" :label-width="120">
				<el-form-item label="当前密码" prop="userPassword">
					<el-input v-model="form.userPassword" type="password" show-password placeholder="请输入当前密码"></el-input>
				</el-form-item>
				<el-form-item label="新密码" prop="newPassword">
					<el-input v-model="form.newPassword" type="password" show-password placeholder="请输入新密码"></el-input>
					<sc-password-strength ref="passwordStrength" v-model="form.newPassword"></sc-password-strength>
					<div class="el-form-item-msg">请输入包含字母、数字的8位以上密码</div>
				</el-form-item>
				<el-form-item label="确认新密码" prop="confirmNewPassword">
					<el-input v-model="form.confirmNewPassword" type="password" show-password placeholder="请再次输入新密码"></el-input>
				</el-form-item>
			</el-form>			
			<el-form style="text-align: center;">
				<el-button v-if="stepActive==0" type="primary" @click="save">提交</el-button>
			</el-form>

			<div v-if="stepActive==2">
				<el-result icon="success" title="修改完成，登录后继续">
					<!-- sub-title="密码修改完成" -->
					<template #extra>
						<el-button type="primary" @click="goLogin">登录</el-button>
					</template>
				</el-result>
			</div>
	
		</div>
	</common-page>
</template>

<script>
	import commonPage from './components/commonPage'
	import scPasswordStrength from '@/components/scPasswordStrength8'
	import setting from '@/views/sys/user/setting'
	import JSEncrypt from 'jsencrypt'
	export default {
		components: {
			scPasswordStrength,
			commonPage
		},
		data() {
			return {
				form: {
					userPassword: "",
					newPassword: "",
					confirmNewPassword: ""
				},
				rules: {
					userPassword: [
						{ required: true, message: '请输入当前密码'}
					],
					newPassword: [
						{ required: true, message: '请输入新密码'},
						{validator: (rule, value, callback) => {
							this.$refs.passwordStrength.strength(value)
							let passLevel = this.$refs.passwordStrength.level
							if (passLevel < 2) {
								callback(new Error('密码复杂度不足'));
							}else{
								callback();
							}
						}}
					],
					confirmNewPassword: [
						{ required: true, message: '请再次输入新密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.newPassword) {
								callback(new Error('两次输入密码不一致'));
							}else{
								callback();
							}
						}}
					]
				},
				publicKey: null,
				stepActive: 0
			}
		},
		async mounted () {
			//导入 public key
			let pubk = await this.$API.system.pub.publicKey.get();
			this.publicKey = (pubk.code == 0) ? pubk.data : null;
		},
		methods: {
			save(){
				this.$refs.form.validate(async valid => {
					if (valid) {
						let d = {
							id : this.form.id,
							oldPassword: '',
							password: ''
						}

						//
						if (this.publicKey) {							
							let encrypt = new JSEncrypt();
							encrypt.setPublicKey(this.publicKey)
							d.oldPassword = encrypt.encrypt(this.form.userPassword)
							d.password = encrypt.encrypt(this.form.newPassword)
						} else {
							d.oldPassword = this.form.userPassword
							d.password = this.form.newPassword
						}

						//
						var res = await setting.api.passwordModify.post(d)
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.stepActive = 2;

							// this.$message.success("重置成功,即将跳转到登录页");							
							// var tt = this;
							// setTimeout(function(){
							// 	//登出
							// 	tt.logout()	
							// 	tt.$router.replace({
							// 		path: '/login'
							// 	})
							// },3000);

						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false
					}
				})
			},
			async logout(){
				await this.$API.auth.logout.post();
			},
			async goLogin(){
				await this.logout()
				this.$router.replace({
					path: '/login'
				})
			}
		}
	}
</script>

<style scoped>


</style>
